package com.vcredit.creditcat.service;

import com.vcredit.creditcat.framework.pagination.Criteria;
import com.vcredit.creditcat.framework.pagination.Page;
import com.vcredit.creditcat.model.MerchantProduct;
import com.vcredit.creditcat.model.ProductCondition;
import com.vcredit.creditcat.model.ext.ProductConditionExt;
import com.vcredit.creditcat.persistence.base.ProductConditionMapper;
import com.vcredit.creditcat.vo.ProductConditionVo;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.cxf.annotations.UseAsyncMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/*
 * @author zhangxingong
 * @version 1.0
 * @since 1.0
*/
@Service
public class ProductConditionService {

  @Autowired
  private  MerchantProductService merchantProductService;
  @Autowired
  private ProductConditionMapper productConditionMapper;

  public Page<ProductConditionExt> findByPage(Criteria criteria) {
    return new Page<ProductConditionExt>(productConditionMapper.findByPageCount(criteria),
        productConditionMapper.findByPage(criteria));
  }

  public List<ProductCondition> find(ProductCondition productCondition) {
    return productConditionMapper.find(productCondition);
  }

  public ProductCondition findById(Long productConditionId) {
    return productConditionMapper.findById(productConditionId);
  }

  public List<ProductCondition> findAll() {
    return productConditionMapper.findAll();
  }

  public void insert(ProductCondition productCondition) {
    productConditionMapper.insert(productCondition);
  }

  public void save(ProductConditionVo productConditionVo) {
    try {
      ProductCondition productCondition = new ProductCondition();
      BeanUtils.copyProperties(productCondition, productConditionVo);
      MerchantProduct name = merchantProductService
          .findByName(productConditionVo.getProductName());
      productCondition.setProductId(name.getProductId());
      productConditionMapper.insert(productCondition);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void update(ProductConditionVo productConditionVo) {

    try {
      ProductCondition productCondition = new ProductCondition();
      BeanUtils.copyProperties(productCondition, productConditionVo);
      MerchantProduct name = merchantProductService
          .findByName(productConditionVo.getProductName());
      productCondition.setProductId(name.getProductId());
      productConditionMapper.update(productCondition);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void removeProductCondition(String ids) {
    for (String productConditionIdString : ids.split(",")) {
      Long productConditionId = Long.parseLong(productConditionIdString);
      productConditionMapper.delete(productConditionId);
    }
  }

}
